package com.taptech.doufu.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.taptech.doufu.R
import com.taptech.doufu.app.WeMediaApplication
import com.taptech.doufu.bean.base.BaseResponseBean
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.constant.IntentKey
import com.taptech.doufu.model.novel.bean.NovelCategoryGroupBean
import com.taptech.doufu.model.novel.bean.NovelCategoryGroupGson
import com.taptech.doufu.net.retrofit.api.ApiClient
import com.taptech.doufu.net.retrofit.api.BaseSubscriber
import com.taptech.doufu.net.retrofit.api.RxJavaHelper
import com.taptech.doufu.ui.adapter.NovelCategoryGroupAdapter
import kotlinx.android.synthetic.main.fragment_category.*

class CategoryFragment :DiaobaoBaseFragment(){


    var cache = mutableMapOf<Int, Fragment>()
    var sexType = "1"



    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater?.inflate(R.layout.fragment_category, container, false)
        sexType = arguments!![IntentKey.INTENT_KEY_SEX_TYPE] as String
        if (arguments!![IntentKey.INTENT_KEY_LOAD_DATA_NOW] as Boolean) {
            initData()
        }
        return view
    }

    fun initData() {
        getCategoryGroup(arguments!![Constant.URL] as String)
    }



    fun getCategoryGroup(url:String){
        ApiClient.getInstance().apiService.getNovelCategoryGroupByUrl(url)
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(object :BaseSubscriber<BaseResponseBean<NovelCategoryGroupGson>>(){

                    override fun onNext(t: BaseResponseBean<NovelCategoryGroupGson>?) {
                        super.onNext(t)
                        rvGroup.layoutManager = LinearLayoutManager(context)
                        val adapter = NovelCategoryGroupAdapter(t?.data?.list)
                        adapter.setOnItemClickListen { position, bean ->
                            switchFragment(position,bean)
                        }
                        rvGroup.adapter = adapter
                        switchFragment(0, t?.data?.list?.get(0))

                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                        Toast.makeText(context,"数据获取失败",Toast.LENGTH_LONG).show()

                    }
                })

    }

    private fun switchFragment(position:Int,item: NovelCategoryGroupBean?) {
        if (item == null){
            return
        }
        val manager = childFragmentManager.beginTransaction()
        for (entry in cache) {
            manager.hide(entry.value)
        }
        if (!cache.containsKey(position)){
            val fragment = CategoryChildFragment.newInstance(item)
            cache[position] = fragment
            manager.add(R.id.layContent,fragment)
        }
        cache[position]?.let { manager.show(it) }
        manager.commit()

    }

    companion object{
        fun newInstance(url: String,sexType:String,loadDataNow:Boolean):CategoryFragment{
            val fragment = CategoryFragment()
            val bundle = Bundle()
            bundle.putString(Constant.URL,url)
            bundle.putBoolean(IntentKey.INTENT_KEY_LOAD_DATA_NOW,loadDataNow)
            bundle.putString(IntentKey.INTENT_KEY_SEX_TYPE,sexType)
            fragment.arguments = bundle
            return fragment
        }
    }

}